//
//  HotViewCell.m
//  Hxcsfw
//
//  Created by 企友软mac on 2017/10/25.
//  Copyright © 2017年 企友软mac. All rights reserved.
//

#import "HotViewCell.h"
#import "MJExtension.h"
#import "HotLbtModel.h"
#import "UIImageView+WebCache.h"
#import "CollectionBtnCell.h"

@interface HotViewCell()<UICollectionViewDelegate,UICollectionViewDataSource> {

    NSArray *arrTitle;
    NSArray *arrImg;
}

@property (weak, nonatomic) IBOutlet UIView *searchView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionBtn;

@end

@implementation HotViewCell

- (void)awakeFromNib {
    [super awakeFromNib];

    _searchView.backgroundColor = WRGBColor(246, 246, 246);
    _searchView.layer.cornerRadius = 15;
    _searchView.layer.borderColor = WRGBColor(228, 228, 228).CGColor;
    _searchView.layer.borderWidth = 1.f;
    _searchView.layer.masksToBounds = YES;

    _arrayOfAds = [NSMutableArray array];
    [_arrayOfAds removeAllObjects];

    for (NSDictionary *dic in [WShareUtil sharedData].arrayOfAds){
         HotLbtModel *model= [HotLbtModel mj_objectWithKeyValues:dic];
         [_arrayOfAds addObject:model];
    }

    _iCarouselView.type = iCarouselTypeCylinder;
    _iCarouselView.decelerationRate =0.4;
    _iCarouselView.scrollSpeed =0.4;
    _iCarouselView.delegate = self;
    _iCarouselView.dataSource = self;

    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.iCarouselView.frame.size.height-20, self.iCarouselView.frame.size.width, 20)];

    pageControl.numberOfPages = _arrayOfAds.count;

    CGSize pointSize = [pageControl sizeForNumberOfPages:_arrayOfAds.count];

    CGFloat page_x = -(pageControl.bounds.size.width - pointSize.width) / 2 ;

    [pageControl setBounds:CGRectMake(page_x + 15, pageControl.bounds.origin.y,
                                      pageControl.bounds.size.width, pageControl.bounds.size.height)];
    pageControl.currentPageIndicatorTintColor = [UIColor grayColor];
    pageControl.pageIndicatorTintColor = [UIColor brownColor];
    pageControl.userInteractionEnabled = NO;

    self.pageControl = pageControl;

    [self.iCarouselView addSubview:self.pageControl];

    [self addTimer];

    arrTitle = @[@"扫码查验",@"手机拍服务",@"在线客服",@"更多服务",@"问题分类",@"图解问吧"];

    arrImg = @[@"hot_QR.png",@"hot_picture.png",@"ys_15.png",@"hot_more2.png",@"hot_QR.png",@"hot_picture.png"];

    [_collectionBtn registerNib:[UINib nibWithNibName:@"CollectionBtnCell" bundle:nil] forCellWithReuseIdentifier:@"CollectionBtnCell"];

    _collectionBtn.backgroundColor = [UIColor whiteColor];
    _collectionBtn.delegate = self;
    _collectionBtn.dataSource = self;
}

-(void)addTimer{
    self.timer=[NSTimer scheduledTimerWithTimeInterval:4.0f target:self selector:@selector(nextCarousel) userInfo:nil repeats:YES];
}

//自动播放下一条广告功能
-(void)nextCarousel{
    NSInteger page=0;
    if (self.pageControl.currentPage == self.pageControl.numberOfPages-1) {
        page=0;
    }else{
        page=self.pageControl.currentPage+1;
    }
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3f];

    self.pageControl.currentPage=page;
    //设置icarouselAdView
    self.iCarouselView.currentItemIndex=page;
    [UIView commitAnimations];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (IBAction)btnClick:(UIButton *)sender {

    if (self.delegate && [self.delegate respondsToSelector:@selector(delegatBtnClick:)]){
        [self.delegate delegatBtnClick:sender];
    }
}

- (void)dealloc {
    if (self.timer) {
        [self.timer invalidate];
        self.timer=nil;
    }
}

#pragma mark -iCarouselDataSource&&iCarouselDelegate

- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel{

    return [_arrayOfAds count];
}

- (NSInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel{

    return 0;
}

- (CGFloat)carouselItemWidth:(iCarousel *)carousel{

    return carousel.frame.size.width;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view{

    UIImageView *imageView;

    if (carousel == _iCarouselView) {
        if(view == nil){
            view=[[UIView alloc]initWithFrame:self.iCarouselView.bounds];
            imageView=[[UIImageView alloc]initWithFrame:view.bounds];
            [view addSubview:imageView];
        }
        else{
            imageView =[[view subviews] lastObject];
        }

        if ([_arrayOfAds[index] isKindOfClass:[HotLbtModel class]]) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:[_arrayOfAds[index] imgsrc]] placeholderImage:[UIImage imageNamed:@"placeholderImage.png"]];
        }else {
            imageView.image = [UIImage imageNamed:[_arrayOfAds objectAtIndex:index]];
        }
    }
    return view;
}

//点击事件
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(delegatiCarClick:)]){
        [self.delegate delegatiCarClick:index];
    }
}

-(void)carouselDidScroll:(iCarousel *)carousel{
    self.pageControl.currentPage = carousel.currentItemIndex;
}

- (void)carouselWillBeginDragging:(iCarousel *)carousel{
    [self.timer invalidate];
    self.timer=nil;
}

- (void)carouselDidEndDragging:(iCarousel *)carousel willDecelerate:(BOOL)decelerate{
    [self addTimer];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    //    return 19;
    return 4;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *NewCell = @"CollectionBtnCell";
    CollectionBtnCell *collectCell = [collectionView dequeueReusableCellWithReuseIdentifier:NewCell forIndexPath:indexPath];

    collectCell.imgView.image = [UIImage imageNamed:arrImg[indexPath.row]];
    collectCell.lbTitle.text = arrTitle[indexPath.row];
    return collectCell;
}

//设置元素的的大小框
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    UIEdgeInsets top = {0,0,0,0};
    top = UIEdgeInsetsMake(0, 0, 0, 0);
    return top;
}

//设置顶部的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    CGSize size={0,0};
    return size;
}

//设置元素大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    CGSize size = CGSizeZero;

    if (iPhone6_6s) {
        size = CGSizeMake((WSCREENH_WIDTH) / 4.,72);
    }else if (iPhone6Plus_6sPlus) {
        size = CGSizeMake((WSCREENH_WIDTH) / 4.,72);
    }else if (XIPHONE) {
        size = CGSizeMake((WSCREENH_WIDTH) / 4.,72);
    }else if (iPhone5SE){
        size = CGSizeMake((WSCREENH_WIDTH) / 4.,72);
    }else {
        size = CGSizeMake((WSCREENH_WIDTH) / 4.,72);
    }
    return size;
}

//点击元素触发事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    if (self.delegate && [self.delegate respondsToSelector:@selector(delegatiCarClick:)]){
        [self.delegate delegatCollectionBtnClick:indexPath.row];
    }

}


@end
